package com.ujoin.dao.imp;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.UserEx;
import com.ujoin.dao.IUserExDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserEx entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.ujoin.bean.UserEx
 * @author MyEclipse Persistence Tools
 */

public class UserExDAO extends HibernateDaoSupport implements IUserExDAO {
	private static final Logger log = LoggerFactory.getLogger(UserExDAO.class);
	// property constants
	public static final String NICKNAME = "nickname";
	public static final String SEX = "sex";
	public static final String CITY = "city";
	public static final String DISTRICT = "district";
	public static final String HEAD_IMAGE = "headImage";
	public static final String PERSONAL_WORD = "personalWord";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#save(com.ujoin.bean.UserEx)
	 */
	@Override
	public void save(UserEx transientInstance) {
		log.debug("saving UserEx instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#delete(com.ujoin.bean.UserEx)
	 */
	@Override
	public void delete(UserEx persistentInstance) {
		log.debug("deleting UserEx instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findById(java.lang.Integer)
	 */
	@Override
	public UserEx findById(java.lang.Integer id) {
		log.debug("getting UserEx instance with id: " + id);
		try {
			UserEx instance = (UserEx) getHibernateTemplate().get(
					"com.ujoin.bean.UserEx", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findByExample(com.ujoin.bean.UserEx)
	 */
	@Override
	public List findByExample(UserEx instance) {
		log.debug("finding UserEx instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserEx instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from UserEx as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findByNickname(java.lang.Object)
	 */
	@Override
	public List findByNickname(Object nickname) {
		return findByProperty(NICKNAME, nickname);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findBySex(java.lang.Object)
	 */
	@Override
	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findByCity(java.lang.Object)
	 */
	@Override
	public List findByCity(Object city) {
		return findByProperty(CITY, city);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findByDistrict(java.lang.Object)
	 */
	@Override
	public List findByDistrict(Object district) {
		return findByProperty(DISTRICT, district);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findByHeadImage(java.lang.Object)
	 */
	@Override
	public List findByHeadImage(Object headImage) {
		return findByProperty(HEAD_IMAGE, headImage);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findByPersonalWord(java.lang.Object)
	 */
	@Override
	public List findByPersonalWord(Object personalWord) {
		return findByProperty(PERSONAL_WORD, personalWord);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all UserEx instances");
		try {
			String queryString = "from UserEx";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#merge(com.ujoin.bean.UserEx)
	 */
	@Override
	public UserEx merge(UserEx detachedInstance) {
		log.debug("merging UserEx instance");
		try {
			UserEx result = (UserEx) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#attachDirty(com.ujoin.bean.UserEx)
	 */
	@Override
	public void attachDirty(UserEx instance) {
		log.debug("attaching dirty UserEx instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.imp.IUserExDAO#attachClean(com.ujoin.bean.UserEx)
	 */
	@Override
	public void attachClean(UserEx instance) {
		log.debug("attaching clean UserEx instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IUserExDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IUserExDAO) ctx.getBean("UserExDAO");
	}
}